package tool

import (
	"MoSkeleton/config/consts"
	"MoSkeleton/framework/core/dto/request"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	tool2 "MoSkeleton/models/tool"
	"MoSkeleton/services/base"
	"MoSkeleton/web/pogos/request/tool"
	tool3 "MoSkeleton/web/pogos/response/tool"
	"gorm.io/gorm"
)

type GenBizService struct {
	base.BaseService
}

func (g *GenBizService) AddGenBiz(addReq *tool.GenBizAddReq) (bool, error) {
	genBiz := new(tool2.GenBiz)
	if err := utils.CopyStruct(genBiz, addReq); err != nil {
		return false, err
	}
	genBiz.Status = consts.U_NORMAL
	var affectedCount int64
	err := base.BaseDb.Transaction(func(tx *gorm.DB) error {
		result := g.GetDb(tx).Create(genBiz)
		if result.Error != nil {
			return result.Error
		}
		affectedCount = result.RowsAffected
		return nil
	})
	return affectedCount > 0, err
}

func (g *GenBizService) UpdateGenBizById(updateReq *tool.GenBizUpdateReq) (bool, error) {
	genBiz := new(tool2.GenBiz)
	if err := genBiz.GetById(nil, updateReq.ID, genBiz); err != nil {
		return false, err
	}

	if err := utils.CopyStruct(genBiz, updateReq); err != nil {
		return false, err
	}
	var affectedCount int64
	err := base.BaseDb.Transaction(func(tx *gorm.DB) error {
		result := g.GetDb(tx).Model(genBiz).Where("id = ?", updateReq.ID).Save(genBiz)
		affectedCount = result.RowsAffected
		return result.Error
	})
	return affectedCount > 0, err
}

func (g *GenBizService) GetGenBizPage(genBizPageReq *tool.GenBizPageReq) (*response.PageResult, error) {
	db := g.getPageVo(g.GetDb(nil), genBizPageReq)
	var total int64
	result := make([]*tool3.GenBizItemResp, 0)
	if err := db.Count(&total).Offset(genBizPageReq.Offset()).Limit(genBizPageReq.Limit()).Find(&result).Error; err != nil {
		return nil, err
	}
	pageResult := response.NewFromPageInfoAndData(&genBizPageReq.PageInfo, &result, total)
	return pageResult, nil
}
func (g *GenBizService) GetGenBizById(id uint64) (*tool3.GenBizDetailResp, error) {
	detailResp := new(tool3.GenBizDetailResp)
	if id != 0 {
		genBiz := new(tool2.GenBiz)
		if err := genBiz.GetById(g.GetDb(nil), id, genBiz); err != nil {
			return nil, err
		}
		itemResp := new(tool3.GenBizItemResp)
		if err := utils.CopyStruct(itemResp, genBiz); err != nil {
			return nil, err
		}
		detailResp.Biz = itemResp
	}
	var err error
	if detailResp.Classifys, err = genTemplateClassifyService.GetAllClassifys(); err != nil {
		return nil, err
	}
	if detailResp.Datasources, err = genDatasourceService.GetAllDatasources(); err != nil {
		return nil, err
	}
	return detailResp, nil
}

func (g *GenBizService) DeleteGenBizByIds(ids *[]uint64) (bool, error) {
	biz := new(tool2.GenBiz)
	return biz.DeleteByIds(g.GetDb(nil), ids, biz)
}

func (g *GenBizService) ChangeStatus(changeStatusReq *request.ChangeStatusReq) error {
	biz := new(tool2.GenBiz)
	biz.Status = changeStatusReq.Status
	biz.ID = changeStatusReq.ID
	return biz.ChangeStatus(nil, biz)
}

func (g *GenBizService) GetDatasourceByBizId(bizId uint64) (*tool2.GenDatasource, error) {
	db := g.GetDb(nil).Select("d.*")
	db = db.Table("gen_datasource d").Joins("left join gen_biz gb on gb.datasource_id = d.id")
	db.Where("gb.id = ? and d.status= ? and gb.status=?", bizId, consts.U_NORMAL, consts.U_NORMAL)
	datasource := new(tool2.GenDatasource)
	result := db.First(datasource)
	return datasource, result.Error
}

func (g *GenBizService) getPageVo(tx *gorm.DB, genBizPageReq *tool.GenBizPageReq) *gorm.DB {
	db := g.GetDb(tx).Table("gen_biz gb").Select("gb.*, tc.title as classify_title")
	db.Joins("left join gen_template_classify tc on tc.id = gb.classify_id")
	beginTime, ok := genBizPageReq.GetParamValue("beginTime")
	if ok && beginTime != "" {
		if beginTime = genBizPageReq.FormatBeginTime(beginTime); beginTime != "" {
			db.Where("gb.created_at >= ?", beginTime)
		}
	}
	endTime, ok := genBizPageReq.GetParamValue("endTime")
	if ok && endTime != "" {
		if endTime = genBizPageReq.FormatEndTime(endTime); endTime != "" {
			db.Where("gb.created_at <= ?", endTime)
		}
	}
	if genBizPageReq.Title != "" {
		db.Where("gb.title like ?", "%"+genBizPageReq.Title+"%")
	}
	if genBizPageReq.Status != "" {
		db.Where("gb.status = ?", genBizPageReq.Status)
	}
	db.Order("gb.status, gb.list_order, gb.id")
	return db
}
